package com.example.login.view

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.base.app.RouterPath
import com.example.common.event.BusEvent
import com.example.common.event.LiveDataBus
import com.example.common.mvi.BaseMviActivity
import com.example.login.R
import com.example.login.databinding.ActivityRegisterBinding
import com.example.login.intent.AccountViewIntent
import com.example.login.state.AccountUiState
import com.example.login.vm.AccountViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = RouterPath.PATH_REGISTER_PAGE)
class RegisterActivity : BaseMviActivity<ActivityRegisterBinding>() {
    private val mViewModel: AccountViewModel by viewModels()

    private var userName = ""
    private var password = ""
    private var repassword = ""

    override fun getLayoutId(): Int =R.layout.activity_register

    override fun initViews() {
        mDataBinding.ivBack.setOnClickListener {
            this@RegisterActivity.finish()
        }
        mDataBinding.tvTitle.text = "注册"

        //给输入框添加监听事件
        mDataBinding.etAccount.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                userName = s.toString().trim()
            }

            override fun afterTextChanged(s: Editable?) {

            }

        })
        mDataBinding.etPassword.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                password = s.toString().trim()
            }

            override fun afterTextChanged(s: Editable?) {

            }

        })
        mDataBinding.etRePassword.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                repassword=s.toString().trim()
            }

            override fun afterTextChanged(s: Editable?) {

            }

        })

        //登录的点击事件
        mDataBinding.tvRegisterBtn.setOnClickListener {
            registerToServer()
        }

    }

    private fun registerToServer() {
        //校验一下输入的内容
        if (userName.isNullOrEmpty() || password.isNullOrEmpty() || repassword.isNullOrEmpty()){
            showToastMsg("账号密码不能为空！")
            return
        }

        //对密码校验是否一致
        if (password != repassword){
            showToastMsg("输入密码不一致！")
            return
        }

        //开启协程发送意图
        lifecycleScope.launch {
            mViewModel.channel.send(AccountViewIntent.RegisterToServer(userName, password))
        }

    }

    override suspend fun collectUIState() {
        mViewModel.uiState.collect{
            when(it){
                is AccountUiState.RegisterToServerSuccess -> {
                    //发送事件给登录页面，回显新账号和密码
                    LiveDataBus.getDefault<BusEvent>("login_page_new_account").value = BusEvent(it.userEntity, 101)
                    //关闭页面
                    this@RegisterActivity.finish()
                }
                is AccountUiState.RegisterToServerFail -> showToastMsg(it.msg)
                else -> {}
            }
        }
    }
}